import 'dart:convert';
import 'package:appflutterhomeschool/common/common.dart';
import 'package:appflutterhomeschool/model/questions_model.dart';
import 'package:logger/logger.dart';

class NotificationModel {
  int id;

  //班级的 senderid   学生的 user id
  String sender;

  //班级的 clsMapping  学生的 notification
  int clsMapping;

//班级的 datetime  学生的 notification_datetime
  String datetime;
  bool needConfirm;
  List img;

  var receiver;


  //班级名字
  String notificationCls;

  String senderType;

  String receiverType;
  String title;
  String msg;
  String notificationType, notificationString;
  var info;

  int noRead;
  String noWrite;
  int write;
  String read;

  //是否已读
  bool isRead;

  //家长端是否确认
  bool confirmed;

  //家长获取通知详情
  String senderImg;
  String senderName;

  //表单
  List<QuestionsModel> listQuestions;
  //时分
  String timeDivision;


  NotificationModel(
      {this.id,
      this.sender,
      this.senderType,
      this.receiver,
      this.receiverType,
      this.title,
      this.msg,
      this.notificationType,
      this.datetime,
      this.info,
      this.clsMapping,
      this.notificationCls});

  NotificationModel.fromJson(Map<String, dynamic> json) {
    
    if(json['img'] != null){
      if(json['img'] is String){
        img = jsonDecode(json['img']);
        Logger().d(img.toList());
      }
    }
    

    id = json['id'];
    //两种情况
    sender = json['sender'] ?? "${json['user']}";
    //两种字段合成一个
    clsMapping = json['cls_mapping'] ?? json['notification'];
    //是否需要确认（True）
    needConfirm = json['need_confirm'] ?? false;
    //是否确认
    confirmed = json['confirmed'];
    senderImg = json['sender_img'] ?? Constant.nullImage;
    senderName = json['sender_name'] ?? "";
    if (json['datetime'] != null) {
      DateTime dateTime = DateTime.parse(json['datetime']);
      datetime = "${dateTime.month}月${dateTime.day}日";
      timeDivision = "${dateTime.hour}:${dateTime.minute}";
    } else if (json['notification_datetime'] != null) {
      DateTime dateTime = DateTime.parse(json['notification_datetime']);
      datetime = "${dateTime.month}月${dateTime.day}日";
      timeDivision = "${dateTime.hour}:${dateTime.minute}";
    }
    //标题
    title = json['title'] ?? json['notification_title'];
    //内容
    msg = json['msg'] ?? json['notification_msg'];
    if (json['receiver'] != null || json['notification_receiver'] != null) {
      receiver = jsonDecode(json['receiver'] ?? json['notification_receiver']);
    }
    //班级名称
    notificationCls = json['notification_cls'];

    senderType = json['sender_type'] ?? "";

    receiverType = json['receiver_type'] ?? "";
    notificationType = json['notification_type'] ?? "";
    //类型：String  必有字段  备注：通知类型（表单：Form,任务：Task，通知：Notification，讨论:ChatRoom）

    notificationString = notificationType == "Form"
        ? "表单"
        : (notificationType == "Task"
            ? "任务"
            : (notificationType == "Notification"
                ? "通知"
                : notificationType == "ChatRoom" ? "讨论" : ""));

    if (json['info'] != null) {
      info = jsonDecode(json['info'] ?? "");
      List writeList = [];
      List noReadList = [];
//      List noWriteList = [];
//      List readList = [];
      if (info["no_read"] != null) {
        noReadList = (info["no_read"] as List);
        noRead = noReadList.length;
      }

      if (info["no_write"] != null) {
//        noWriteList = (info["no_write"] as List);
      }
      if (info["write"] != null) {
        write = writeList.length;
      }
      if (info["read"] != null) {
//        readList = info["read"] ?? [];
      }
    }
    isRead = json['is_read'] ?? true;
    if (json['questions'] != null) {
      listQuestions = [];
      (json['questions'] as List).forEach((element) {
        listQuestions.add(QuestionsModel.fromJson(element));
      });
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['id'] = this.id;
    data['sender'] = this.sender;
    data['sender_type'] = this.senderType;
    data['receiver'] = this.receiver;
    data['receiver_type'] = this.receiverType;
    data['title'] = this.title;
    data['msg'] = this.msg;
    data['notification_type'] = this.notificationType;
    data['datetime'] = this.datetime;
    data['info'] = this.info;
    data['cls_mapping'] = this.clsMapping;
    data['notification_cls'] = this.notificationCls;
    return data;
  }
}
